Deferred type inference of generic type parameters in function calls to overloaded functions

ABSTRACT

The disclosed embodiments provide a system that facilitates the development and execution of a software program. During runtime of the software program, the system delays type inference on a generic type parameter of an implementation of an overloaded function, wherein the generic type parameter is associated with a type interval containing an unbounded lower limit and one or more self-typed constraints. Upon detecting a type query for a dynamic type of the generic type parameter, the system compares a queried type from the type query with a set of inference choices for the generic type parameter. If the queried type matches an inference choice from the set of inference choices, the system uses the inference choice to perform type inference on the generic type parameter.

RELATED APPLICATIONS

The subject matter of this application is related to the subject matterin a co-pending non-provisional application by inventors David Chase,Guy Steele, Karl Naden, Justin Hilburn and Victor Luchangco, entitled“Fast Dispatch Predicate for Overloaded Functions with Generic TypeHierarchies that Lack Contravariance,” having Ser. No. 13/601,730, andfiling date 31 Aug. 2012.

The subject matter of this application is related to the subject matterin a co-pending non-provisional application by inventors Karl Naden,Justin Hilburn, David Chase, Guy Steele, Victor Luchangco and EricAllen, entitled “Dispatch Predicate for Overloaded Functions using TypeIntervals,” having Ser. No. 13/601,745, and filing date 31 Aug. 2012.

The subject matter of this application is also related to the subjectmatter in a co-pending non-provisional application by inventors KarlNaden, David Chase and Justin Hilburn, entitled “Type Inference ofGeneric Type Parameters in Overloaded Functions using Type Intervals andInference Directions,” having Ser. No. 13/601,766, and filing date 31Aug. 2012.

BACKGROUND

1. Field

The disclosed embodiments relate to function overloading in programminglanguages. More specifically, the disclosed embodiments relate totechniques for deferring type inference of generic type parameters infunction calls to overloaded functions.

2. Related Art

Programming languages may support function and/or method overloading, inwhich multiple methods within an object and/or functions declared withinthe same scope share the same name. Such name sharing may facilitate theidentification and/or grouping of functions and/or methods that performconceptually similar tasks but operate on different types and/or amountsof data.

During invocation of an overloaded function and/or method, a programminglanguage may dispatch a function call to the function and/or method byselecting an implementation of the function and/or method based on thetypes and/or number of arguments from the function call. For example,the programming language may select the most specific implementationfrom a set of candidate implementations of the function and/or methodthat are accessible and applicable. Moreover, the programming languagemay use multiple dispatch, which resolves the function call based on theruntime types of the function call's arguments.

However, an overloaded function and/or method may include one or moregeneric functions containing parameterized types. Because the genericfunctions may accept parameters from the same and/or overlapping sets oftypes, the generic functions may complicate the determination ofspecificity and/or applicability during dispatch of a function call tothe function and/or method. The function call may also include generictype parameters, which must also be inferred for correct dispatching ofthe function call. In turn, the additional complexity and/or computationassociated with generic functions and/or generic type parameters inoverloaded functions may increase the overhead associated with runtimedispatch of function calls to the overloaded functions.

Hence, what is needed is a mechanism for performing multiple dispatch offunction calls associated with generic type hierarchies and/or typeinference on generic type parameters associated with the function calls.

SUMMARY

The disclosed embodiments provide a system that facilitates thedevelopment and execution of a software program. During runtime of thesoftware program, the system delays type inference on a generic typeparameter of an implementation of an overloaded function, wherein thegeneric type parameter is associated with a type interval containing anunbounded lower limit and one or more self-typed constraints. Upondetecting a type query for a dynamic type of the generic type parameter,the system compares a queried type from the type query with a set ofinference choices for the generic type parameter. If the queried typematches an inference choice from the set of inference choices, thesystem uses the inference choice to perform type inference on thegeneric type parameter.

In some embodiments, if the queried type does not match any of theinference choices, the system further delays type inference on thegeneric type parameter.

In some embodiments, using the inference choice to perform typeinference on the generic type parameter involves using the inferencechoice as an updated lower limit of the type interval, and choosing abinding for the generic type parameter based on the updated lower limit.

In some embodiments, the binding is further chosen based on a set ofconstraints associated with the generic type parameter.

In some embodiments, choosing the binding for the generic type parameterbased on the updated lower limit involves using the inference choice asthe binding for the generic type parameter if the generic type parameteris not associated with a set of constraints.

In some embodiments, the set of inference choices is bounded by the typeinterval.

In some embodiments, the type interval also includes an upper limit.

BRIEF DESCRIPTION OF THE FIGURES

FIG. 1 shows a schematic of a system in accordance with the disclosedembodiments.

FIG. 2 shows the selection of an implementation of an overloadedfunction for invocation by a function call in accordance with thedisclosed embodiments.

FIG. 3 shows type inference on generic type parameters of animplementation of an overloaded function in accordance with thedisclosed embodiments.

FIG. 4 shows a flowchart illustrating the process of facilitating thedevelopment and execution of a software program in accordance with thedisclosed embodiments.

FIG. 5 shows a computer system in accordance with the disclosedembodiments.

In the figures, like reference numerals refer to the same figureelements.

DETAILED DESCRIPTION

The following description is presented to enable any person skilled inthe art to make and use the embodiments, and is provided in the contextof a particular application and its requirements. Various modificationsto the disclosed embodiments will be readily apparent to those skilledin the art, and the general principles defined herein may be applied toother embodiments and applications without departing from the spirit andscope of the present disclosure. Thus, the present invention is notlimited to the embodiments shown, but is to be accorded the widest scopeconsistent with the principles and features disclosed herein.

The data structures and code described in this detailed description aretypically stored on a computer-readable storage medium, which may be anydevice or medium that can store code and/or data for use by a computersystem. The computer-readable storage medium includes, but is notlimited to, volatile memory, non-volatile memory, magnetic and opticalstorage devices such as disk drives, magnetic tape, CDs (compact discs),DVDs (digital versatile discs or digital video discs), or other mediacapable of storing code and/or data now known or later developed.

The methods and processes described in the detailed description sectioncan be embodied as code and/or data, which can be stored in acomputer-readable storage medium as described above. When a computersystem reads and executes the code and/or data stored on thecomputer-readable storage medium, the computer system performs themethods and processes embodied as data structures and code and storedwithin the computer-readable storage medium.

Furthermore, methods and processes described herein can be included inhardware modules or apparatus. These modules or apparatus may include,but are not limited to, an application-specific integrated circuit(ASIC) chip, a field-programmable gate array (FPGA), a dedicated orshared processor that executes a particular software module or a pieceof code at a particular time, and/or other programmable-logic devicesnow known or later developed. When the hardware modules or apparatus areactivated, they perform the methods and processes included within them.

The disclosed embodiments provide a method and system for facilitatingthe development and execution of a software program. During developmentof the software program, source code for the software program may becreated using a programming language. The source code may then becompiled into an executable form to enable the execution of the softwareprogram.

More specifically, the disclosed embodiments provide a method and systemfor facilitating the development and execution of a software program ina programming language with a flexible system of generic trait and classobject types, generic functions and methods, overloaded functions andmethods, type inference, and static type checking. To support suchfeatures, the programming language may provide a well-defined andefficient implementation of overloaded dispatch and type inference.

First, the programming language may perform runtime dispatch of functioncalls associated with generic type hierarchies. During runtime of thesoftware program, a function call to an overloaded function may beresolved based on a partial order of implementations of the overloadedfunction and the applicability of one or more of the implementations tothe function call. For example, the partial order may correspond to amost-to-least specific order, such that the implementation selected forinvocation by the function call is the most specific implementation thatis applicable and accessible to the function call.

Second, the programming language may perform type inference on generictype parameters of the implementation during dispatch of the functioncall. During type inference, the programming language may obtain a typeinterval for a generic type parameter of the selected invocation, whichcontains an unbounded lower limit and one or more self-typedconstraints. Because the unbounded lower limit may prevent theprogramming language from searching upwards for a unique and/ormost-specific binding for the generic type parameter, the unboundedlower limit may result in more than one type-correct inference choicefor the generic type parameter.

As a result, the programming language may delay type inference on thegeneric type parameter until a type query for a dynamic type of thegeneric type parameter is detected. The queried type from the type querymay then be compared with a set of inference choices for the generictype parameter. If the queried type matches an inference choice from theset of inference choices, the inference choice may be used to performtype inference on the generic type parameter. If the queried type doesnot match any of the inference choices, type inference on the generictype parameter may be further delayed.

FIG. 1 shows a schematic of a system in accordance with the disclosedembodiments. The system includes a compilation manager 102 and runtimesystem 104 that may be used to manage the development and execution of asoftware program 110. Software program 110 may correspond to astandalone application, operating system, enterprise application,database, library, device driver, and/or other type of software. Inaddition, software program 110 may be executed in a variety ofenvironments. For example, software program 110 may be executed on asingle desktop computer or workstation, or software program 110 may bedistributed across multiple servers within a data center. Along the samelines, software program 110 may be executed sequentially or in parallelon one or more processors and/or processor cores.

In one or more embodiments, compilation manager 102 and runtime system104 are associated with a programming language with a flexible system ofgeneric trait and class object types, generic functions and methods,overloaded functions and methods, type inference, and static typechecking. For example, compilation manager 102 and runtime system 104may enable the development, compilation, and/or execution of code forsoftware program 110 written in the Fortress programming language. Whilethe discussion below relates to the Fortress programming language, thoseskilled in the art will appreciate that the operation of compilationmanager 102 and runtime system 104 may be used with other programminglanguages with similar type hierarchies and/or features.

The Fortress language type system includes objects, traits, tuples,arrows, and “Any.” Traits and objects form one hierarchy, tuples formanother, and arrows form a third; these three hierarchies are disjoint.Fortress types are partially ordered by a subtype relation, alsoexpressed with the verb “extends” which appears in trait and objectdeclarations, and the symbol “<:” which appears semantically. Type X isa subtype of Y (“X extends Y”, “X<: Y”) if every value that is an X isalso a Y. Trait and object subtyping is declared (nominal); tuple andarrow subtyping is structural. Subtyping is reflexive, transitive, andantisymmetric: X<: X, X<: Y ΛY<: Z=>X<: Z, and X<: Y ΛY<: X=>X=Y.

The trait and object hierarchy is rooted at the trait “Object,” whichextends “Any.” Within the trait and object hierarchy, objects and traitsboth may extend traits, but nothing may extend an object type. Traitextension is part of an object or trait's declaration:

trait Shape extends Object

trait Polygon extends Shape

trait Rectangle extends Polygon

trait RegularPoly extends Polygon

trait Square extends {Rectangle, RegularPoly}

object Cartesian (x:RR, y:RR) extends Point

object Polar (theta:RR, d:RR) extends Point

A trait declaration may use a “comprises” clause to limit the traits andobjects that can directly extend it:

trait Point comprises {Cartesian, Polar}

trait Quadrilateral comprises

-   -   {Trapezoid, Kite, Irregular, Concave}

trait Kite extends Quadrilateral

trait Trapezoid extends Quadrilateral

trait Parallelogram extends Trapezoid comprises

-   -   {Rectangle, Rhombus, Rhomboid}

trait Rectangle extends Parallelogram comprises

-   -   {Oblong, Square}

trait Rhombus comprises {Square, NotSquare} extends

-   -   {Parallelogram, Kite}

trait Square extends {Rectangle, Rhombus}

Comprised traits may be further extended (Trapezoid, Parallelogram,Rhombus); are not necessarily disjoint (Rhombus extends both Kite andParallelogram, which in turn extends Trapezoid); and may inherit fromother traits (Rhombus comprises Square, Square also extends Rectangle).

Tuple types are sequences of zero, two, or more (but not one) othertypes, including tuples, arrows, traits, and objects. All tuple typesextend “Any,” and a tuple type with X=(X₁, X₂, . . . X_(N)) extendsY=(Y₁, Y₂, . . . Y_(M)) if and only if N=M and X_(i)<: Y_(i) for 1≦i≦N.That is, equal-length tuples are covariant in the types of theirelements. The zero-length tuple is also known as “void” with “voidtype.”

Arrow types are the types of functions. The arrow type D→R combines adomain type D and a range (return) type R. Arrow types are covariant intheir range and contravariant in their domain type. For example, D1→R1<:D2→R2 if and only if R1<: R2 (note the order 1, 2, therefore covariant)and D2<: D1 (note the order 2, 1, therefore contravariant).

By construction, Fortress types may be divided into various disjointsets—arrows are never tuples or traits, and traits are never tuples.Because object types cannot be further extended, any object type isknown to exclude any trait that it is not declared to (transitively)extend, as well as all other object types. Fortress also allows anexplicit declaration of exclusion on trait types; if T excludes U, thenno type may extend both T and U. Declared exclusions extend naturallyinto tuple and arrow types; if R excludes S, then D→R excludes E→S, andif S_(k) excludes T_(k), then (S₁, . . . , S_(k), . . . , S_(a))excludes (T₁, . . . , T_(k), . . . , T_(a).)

Fortress also has generic trait and object types (pedantically speaking,first-order type operators) that combine types to form new types.Generic type declarations may include subtype constraints in theirparameters:

trait Vector[\T extends Number\]

trait SortedList[\T extends Comparable[\T\] \]

trait Option[\T extends Any\]

Generic traits may also have a declared variance in their variousparameters. Immutable data types like list and option can be covariant,and arrow-like types can be contravariant in their domain:

trait SortedList[\covariant T extends

-   -   Comparable[\T\] \]

trait Option[\covariant T extends Any\] trait

-   -   ArrowLike[\contravariant D,    -   covariant R, covariant E\]        Unless otherwise specified, two instances of a generic trait are        disjoint if they differ in any one of their static parameters.        For example, although Integer<: Number, Matrix[Integer] is not a        subtype of Matrix[Number]. Such generic types are invariant (or,        more precisely, invariant with respect to each of their static        parameters).

Every constructed type has a name of the form Stem[T1, T2, . . . , Tn],where Stem is an identifier and T1, T2, . . . , Tn is a (possibly empty)sequence of types. If the sequence of types is empty (that is, n=0),then Stem[ ] may be abbreviated as simply Stem. Strictly speaking,however, the stem is the name of a generic type, that is, a type thathas parameters. When specific type arguments a1, a2, . . . , an areprovided, then Stem[a1, a2, . . . , an] is said to be a type that is aninstance of the generic type named by the Stem. For example, List[T] isa generic type, whose values are lists whose elements are all of type T.The identifier “List” is the stem of this generic type. The generic typehas one type parameter. List [String] is a specific instance of thisgeneric type, namely the type whose values are lists whose elements areall of type String. (The type String, in turn, is understood to meanString[ ], the unique instance of the generic type having zero typeparameters and whose stem is “String.”)

A particular idiom used in Fortress is the “self-typed generic,” where ageneric in T also comprises exactly T. This usually corresponds to aproperty of a binary operator method such as “Comparable” or“AssociativePlus”:

trait Comparable[\T\] comprises T opr < (self, other:T) end traitAssociativePlus[\T\] comprises T opr + (self, other:T) endBecause the only subtype of Comparable[\T\] is T, the two types includeexactly the same sets of values, and are in some sense the same type.

Type inference applied to Fortress software programs may yield typesthat cannot be directly expressed in the source code. Analysis,optimization, and implementation are all easier to reason about when thetypes form a lattice, not just a partial order, and there are caseswhere the lattice properties are also obvious to the programmer, and mayeven reflect intent. This requires union and intersection types toensure that join and meet operations are defined, plus a “bottom” type.Because no values actually have bottom type, the appearance of a bottomtype indicates code that is surely “dead.” When two types exclude eachother, their meet is bottom.

At join points in a program (either flow join points or inference joinpoints), “union” types may appear. In the presence of contravariantgeneric types, “intersection” types may appear. In this example, z'sstatic type is X ∪Y:

x:x= . . .

y:Y= . . .

z=if is Raining( ) then x else y end

In this example, a call to a generic function f results in staticinference T=X ∪Y:

f[\T\] (a:T, b:T)= . . .

x:X= . . .

y:Y= . . .

f(x,y)

When contravariant types are joined, intersection types can result.Here, the statically inferred type for T is X ∩ Y, because X ∩ Y→( ) isa supertype of both X→( ) and Y→( ):

f[\T\] (g:T->( ), h:T->( ))= . . .

x:X->( )= . . .

y:Y->( )= . . .

f(x, y)

Typecase statements are another source of intersection types. In eachguarded clause of a typecase statement, the type of the testedexpression is known to be both its static type (outside the guard) andits guard type; that is, the intersection of those two types. In thisexample, the type oft is known to be X # Y:

x:X = ... typecase x of t:Y => ... t ... end

Given a covariant generic G, G[\A\] ∪G[\B\]<: G[\A ∪B\]. Equality doesnot hold. Consider a set S={“cat”, 11}; S is a Set[\String # Number\]but is not a Set[\String\] ∪ Set[\Number\]. For intersections ofcovariant generics, given restrictions on types listed below, equalitydoes hold: G[\A\] ∩ G[\B\]=G[\A ∩ B\].

Restrictions on Fortress types include the following:

-   -   No cycles in extends relationship.    -   Covariant and contravariant use restriction, including        supertypes.

Contravariant type parameters may only appear in contravariant contextand covariant type parameters may only appear in covariant context.

-   -   Minimal instance of generic ancestors: If S<: G[\T^(→)\], then        there exists U^(→) such that for all T^(→) where S<: G[\T^(→)\]:        -   if G's ith static parameter is invariant, then U_(i)=T_(i).        -   if G's ith static parameter is covariant, then U_(i)<:            T_(i).        -   if G's ith static parameter is contravariant, then T_(i)<:            U_(i).        -   G[\U^(→)\] is the minimal instance of G that S extends.    -   Generic of bottom is bottom: G[\∞\]=∞.    -   Finite depth: Foo[\T\] extends T is prohibited.    -   Finite depth: T<: G[\ . . . \] and T<: H[\ . . . \]. If G[\A\]<:        H[\B\] exists then H[\C\]<: G[\D\] does not exist.    -   Allowed type constraints:        -   T1<: T2 (T1 extends T2).        -   T1<: K (T1 extends type constant expression K).        -   T1<: G[\T2, T3\] (T1 extends some instantiated generic type            whose instantiation contains type parameters).        -   Acyclic type constraints: for a set of type constraints on a            generic type or function, there is an order such that each            static parameter only appears on the right-hand-side of            constraints following its mention on the left-hand-side of a            constraint. By default, the restriction that constraints are            written in such an order is imposed. For example, [T1, T2<:            T1, T3<: Pair[T1,T2]] is permitted because no type is used            on the right-hand-side of a constraint until after the            constraint where it appears on the left. Self-typed            constraints are an exception to this rule; it is permitted            to declare that T1<: SomeSelfType[T1]. Because of the            different subtyping structure of self-types, this is really            more of an equality constraint than an inequality            constraint.        -   Self-types meet: if T<: U=S[\U\] and T<: V=S[\V\] then T<:            S[\meet(U, V)\] and meet(U, V) must be a declared (not            intersection) type. In practice, this means that the            instantiations of a particular self-typed generic must form            a forest.

Fortress also has overloaded functions. Whenever more than one functionwith the same name appears in a scope, an overloaded function results,and the same-named functions become implementations that are chosen whenthe overloaded function is invoked. Overloaded functions may also beexported, either as explicitly overloaded functions (the multipleimplementations appear in an API) or as the most general member of a setof implementations. When an overloaded function is called at runtime,the most specific of the set of implementations is chosen, consideringall arguments to the function. The implementations to an overloadedfunction in a given scope must satisfy two rules to guaranteenon-ambiguity and type safety.

First, the meet rule ensures that dispatch is unambiguous. Given twoimplementations f1 and f2 of the overloaded function f, either thedomain of f1 excludes the domain of f2, or else f contains animplementation f3 whose domain is the meet of f1 and f2's domains (f3may be f1, f2, or some other implementation). Second, the subtype ruleensures type safety; if f1's domain is a subtype of f2's domain, thenf1's range must be a subtype of f2's range.

Fortress also supports generic type parameters in overloaded functions.Because generic type schema are not ordinary types, meet and subtypemust be extended to cover this case. Dynamic subtype tests used tochoose between ordinary types must also be extended to handle dispatchin the presence of generics.

As shown in FIG. 1, software program 110 may include an overloadedfunction 106, method, and/or subroutine that is invoked by a functioncall 112. Overloaded function 106 may include a set of implementations114-116 in the same scope that share the same name but contain differenttypes and/or numbers of parameters. During runtime of software program110, compilation manager 102 and/or runtime system 104 may dispatchfunction call 112 by selecting an implementation from implementations114-116 for invocation by function call 112.

To accommodate features of the programming language associated withsoftware program 110, compilation manager 102 and/or runtime system 104may provide a dispatch predicate for overloaded function 106 and/orother overloaded functions with generic type hierarchies and/orparameters that are invariant, covariant, and/or contravariant. Inparticular, compilation manager 102 and/or runtime system 104 may selectan implementation for invocation by function call 112 based on anapplicability of the implementation to function call 112 and a partialorder of implementations 114-116, as discussed in further detail belowwith respect to FIG. 2. Compilation manager 102 and/or runtime system104 may additionally perform type inference on generic type parametersof the selected implementation, as discussed further below with respectto FIG. 3.

FIG. 2 shows the selection of an implementation 204 of an overloadedfunction (e.g., overloaded function 106 of FIG. 1) for invocation by afunction call 112 in accordance with the disclosed embodiments. Asmentioned above, implementation 204 may be selected based on a partialorder 202 of implementations (e.g., implementations 114-116 of FIG. 1)for the overloaded function. For example, static analysis may be used toprovide partial order 202 and enforce the language-level restrictions ontypes described above.

In addition, partial order 202 may correspond to a most-to-leastspecific order, so that dynamic dispatch of function call 112 may bereduced to testing a can-apply predicate for each implementation until amatch is found. This reduces overload resolution to the simpler problemof determining if an implementation is applicable to the actualparameters supplied by function call 112. The same dispatch strategy mayalso be used whenever there is an order among an overloaded function'simplementations (e.g., a user-specified preference for dispatch, usingthe most-recently-written applicable member, etc.).

In one or more embodiments, function call 112 is resolved using adispatch predicate that determines an applicability 214 ofimplementation 204 to function call 112 based on one or more dynamictypes 206-208 for arguments of function call 112 and one or moresignature types 210-212 of implementation 204. For example, the dispatchpredicate may return false if implementation 204 is not applicable tofunction call 112 and true if implementation 204 is applicable tofunction call 112. If implementation 204 is not applicable, the dispatchpredicate is repeated for one or more subsequent implementations inpartial order 202 until an applicable implementation is found. Ifimplementation 204 is applicable, the dispatch predicate may providebindings 218 for any static type parameters present in signature types210-212.

Dynamic types 206-208 may be type constant expressions which lack typevariables but may contain tuples, arrows, instantiated-with-constantsgeneric types, unions, Any, trait, and object types. Signature types210-212 may include type variables, tuples, arrows,instantiated-with-signatures generic types, and type constants. Asignature type may contain a union type appearing as a type constant,but elements of the union type cannot contain type variables. All typesare also expressed in their canonical form, so it is known that if A andB are both terms of a union type, then neither is a subtype of theother.

During the determination of applicability 214, each dynamic type 206-208for an argument of function call 112 is compared to the correspondingsignature type 210-212 of implementation 204 based on a variance 220-222(e.g., covariant, invariant, contravariant) of the dynamic type. If asubtype relationship between the dynamic type and the signature typedoes not conform to the variance, implementation 204 is determined to benot applicable to function call 112. If the subtype relationshipconforms to the variance, implementation 204 is determined to beapplicable to function call 112, and a set of initial limits 216 on oneor more generic type parameters of implementation 204 are determined.Dispatch predicates for overloaded functions using type intervals aredescribed in a co-pending non-provisional application by inventors KarlNaden, Justin Hilburn, David Chase, Guy Steele, Victor Luchangco andEric Allen, entitled “Dispatch Predicate for Overloaded Functions usingType Intervals,” having Ser. No. 13/601,745, and filing date 31 Aug.2012, which is incorporated herein by reference.

For example, the dispatch predicate for determining applicability 214may be implemented using a “match” function that takes three parameters:the first (“T”) is a signature type (e.g., signature types 210-212) thatmay contain unbound static (e.g., generic) type parameters, the second(“V”) is the variance (encoded as +1, 0, and −1, where positive iscovariant, 0 is invariant, and negative is contravariant), and the third(“A”) is an actual dynamic type (e.g., dynamic types 206-208) to berelated to the signature type, subject to the specified variance. The“match” function may also rely on the set “S” of type names beinginferred, and for each type name “t εS” augments upper and lower boundconstraint sets “Ut” and “Lt” (e.g., initial limits 216).

If the type and signature cannot be related, then “match” fails. If thedynamic type and signature type can be related, “match” returns normallyand adds necessary constraints to the upper and lower bound sets (e.g.,initial limits 216), which are the input to type inference of thegeneric type parameters. Initial limits 216 may then be used todetermine bindings 218 during type inference of generic type parameters,as described in a co-pending non-provisional application by inventorsKarl Naden, David Chase and Justin Hilburn, entitled “Type Inference ofGeneric Type Parameters in Overloaded Functions using Type Intervals andInference Directions,” having Ser. No. 13/601,766, and filing date 31Aug. 2012, which is incorporated herein by reference.

On the other hand, initial limits 216 may include a type interval withan unbounded lower limit for a generic type parameter, which results inmore than one type-correct inference choice for the generic typeparameter. If such a type interval is encountered, type inference on thegeneric type parameter may be deferred and/or delayed until informationregarding the use of the generic type parameter (e.g., a type query fora dynamic type of the generic type parameter) is available. Theinformation may then be used to update the lower limit of the typeinterval and perform type inference on the generic type parameter, asdiscussed in further detail below with respect to FIG. 3.

FIG. 3 shows type inference on generic type parameters of animplementation of an overloaded function in accordance with thedisclosed embodiments. As described in the above-referenced application,type inference may be performed after the implementation is initiallydetermined to be applicable to a function call associated with theoverloaded function (e.g., using the “match” function).

During type inference, a binding 316 for a generic type parameter of theimplementation may be selected based on a type interval 302 and aninference direction (not shown) for the generic type parameter. Typeinterval 302 may be obtained from the “match” function described in theabove-referenced applications.

As shown in FIG. 3, type interval 302 may include an unbounded lowerlimit 304 and one or more upper limits 306. Unbounded lower limit 304may lack restrictions on the lower bounds of the generic type parameter.For example, unbounded lower limit 304 may include a “bottom” type,which is a subtype of every other type.

In addition, the generic type parameter may be associated with one ormore self-typed constraints 320, which may require an upward search ofself-types to adjust limits (e.g., unbounded lower limit 304, upperlimits 306) in type interval 302 before binding 316 can be selected.However, the upward search may not be tractable from unbounded lowerlimit 304, resulting in multiple type-correct inference choices 318 forthe generic type parameter that may be made from type interval 302. Forexample, inference choices 318 may include self-types that are boundedby upper limits 306 of type interval 302.

In one or more embodiments, type inference on the generic parameter isdelayed and/or deferred until information regarding use of the generictype parameter is available. More specifically, the information may beobtained as a queried type 308 from a type query for a dynamic type ofthe generic type parameter.

To determine if queried type 308 is relevant to the generic typeparameter, queried type 308 may be compared with inference choices 318.If queried type 308 matches an inference choice from inference choices318, queried type 308 may be used as an updated lower limit 312 of a newtype interval 310 for the generic type parameter, and type inference ofthe generic type parameter may be performed using updated lower limit312 and one or more upper limits 314 from type interval 310 and/or a setof constraints for the generic type parameter. If queried type 308 doesnot match any inference choices 318, queried type 308 may not berelevant to the generic type parameter, and type inference of thegeneric type parameter may continue to be deferred and/or delayed.

Type inference of generic type parameters using the techniques describedabove may be illustrated with the following example. Suppose thatEquality[T] is a covariant self-type that provides the ability to testequality:

trait Equality[covariant T] comprises T eq(other:T) endIn addition, “f” is an overloaded function:

f(x:Any) :Any = x f[T <: Equality[T]] (g:T −> Integer, d:T) :(T,T)−>Integer = fn(x:T,y:T) => if x.eq(y) then g(d) else g(x) + g(y)end

One very general implementation of “f” takes “x” with type Any (in otherwords, all possible inputs) and returns it. The second implementationapplies when the input happens to be a 2-element tuple of function anddefault input, generic in T, where T must be a self-type with Equality,and that returns a function from tuples of (T, T) to integers. A body isprovided to show that code implementing this contrived type signatureis, in fact, possible. The second implementation is clearly morespecific, and the pair of functions clearly obeys both the meet rule andreturn type rule; this is a legal overload using the Fortress rules.

Now consider the following declarations:

trait Q

-   -   foo ( ): Integer

end

h(q:Q):Integer=q.foo( )

trait P extends {Equality[P], Q} . . . end

trait w extends {Equality [W], Q} . . . end

“h” is a function mapping Q to Integer. Because “P” and “W” both extend“Q,” “g” may also be regarded as a function mapping “P” to Integer andmapping “Q” to Integer (that is, “Q->Integer” extends both “P->Integer”and “Q->Integer”). Furthermore, “P” and “Q” are both self-types withEquality; a pair of P's may be tested for Equality, and a pair of Q'smay be tested for Equality. However, a “P” and a “Q” may not be testedfor Equality.

Next, consider this invocation of “f” and its dynamic dispatch:

a:Any=f(h)

There are two entrypoints, and the more specific is tested first forapplicability:

f[T<: Equality[T]](g:T->Integer):(T,T)->Integer

Notice that the type parameter T appears only in contravariant context;therefore T's inference goal is “upper.” Pattern matching determinesthat “h” matches the structure of parameter “g”; it is a functionreturning Integer, and the constraint on T is that it lies within theinterval [Bottom, Q].

Finally, consider the self-type constraint that must hold on T fordispatch to succeed; there needs to be at least one type T′ above Bottombut below “Q” with the property that T′ is a self-type with Equality. Inthis case, there are two (“P” and “W”), and two is more than one, so thedispatch test succeeds. Unfortunately, with two choices, there is noclear choice for the inference step.

One way to resolve this problem is simply to defer the choice until atsome later operation the value makes it necessary. The result type ofthe overloaded function is “Any”; regarded as an Any, there is noparticular need to make either choice. If, however, the result value issubject to a type query that matches one of its possible instantiations,then that instantiation is chosen. For example:

if a instanceof (P,P)->Integer then

-   -   (*) T can be P, therefore T is P (permanently)    -   . . .

end

In the general case, other types may depend on the choice for T;dispatch predicate testing must ensure that each of the possible choicesfor T is compatible with successful inference of other type parameters,but computation of the exact choice(s) is suspended until a subsequenttype query.

FIG. 4 shows a flowchart illustrating the process of facilitating thedevelopment and execution of a software program in accordance with thedisclosed embodiments. In one or more embodiments, one or more of thesteps may be omitted, repeated, and/or performed in a different order.Accordingly, the specific arrangement of steps shown in FIG. 4 shouldnot be construed as limiting the scope of the embodiments.

Initially, type inference on a generic type parameter of animplementation of an overloaded function is delayed during runtime ofthe software program (operation 402). The generic type parameter may beassociated with a type interval containing an unbounded lower limit andone or more self-typed constraints. As a result, the generic typeparameter may have more than one type-correct inference choice.

Type inference of the generic type parameter and/or other generic typeparameters may continue to be performed (operation 404) and/or delayed.For example, type inference of the generic type parameter(s) may beperformed and/or delayed until bindings are chosen for the generic typeparameter(s) and/or the software program is no longer running Duringtype inference, a type query for a dynamic type of the generic typeparameter may be detected (operation 406). If no type query is detected,type inference on the generic type parameter may continue to be delayed(operation 402) while type inference of generic type parameters of thesoftware program is enabled (operation 404).

If the type query is detected, a queried type from the type query iscompared with a set of inference choices for the generic type parameter(operation 408) to determine if the queried type matches an inferencechoice (operation 410) from the set of inference choices. For example,the queried type may be compared with inference choices that are boundedby one or more upper limits of the type interval.

If the queried type matches the inference choice, the inference choiceis used to perform type inference on the generic type parameter(operation 412). For example, the inference choice may be used as anupdated lower limit of the type interval, and a binding for the generictype parameter may be chosen based on the updated lower limit and/or aset of constraints associated with the generic type parameter using thetechniques described in the above-referenced applications. If there areno constraints, the inference choice may be used as the binding for thegeneric type parameter.

If the queried type does not match the inference choice, type inferenceon the generic type parameter may continue to be delayed (operation 402)until a type query results in an inference choice that can be used toperform type inference on the generic type parameter (operations404-412). Alternatively, type queries that are relevant to the generictype parameter may not be detected, and type inference of the generictype parameter beyond the unbounded lower limit may not occur duringruntime of the software program.

FIG. 5 shows a computer system 500 in accordance with the disclosedembodiments. Computer system 500 may correspond to an apparatus thatincludes a processor 502, memory 504, storage 506, and/or othercomponents found in electronic computing devices. Processor 502 maysupport parallel processing and/or multi-threaded operation with otherprocessors in computer system 500. Computer system 500 may also includeinput/output (I/O) devices such as a keyboard 508, a mouse 510, and adisplay 512.

Computer system 500 may include functionality to execute variouscomponents of the present embodiments. In particular, computer system500 may include an operating system (not shown) that coordinates the useof hardware and software resources on computer system 500, as well asone or more applications that perform specialized tasks for the user. Toperform tasks for the user, applications may obtain the use of hardwareresources on computer system 500 from the operating system, as well asinteract with the user through a hardware and/or software frameworkprovided by the operating system.

In one or more embodiments, computer system 500 provides a system forfacilitating the development and execution of a software program. Thesystem may include a compilation manager and a runtime system. Thecompilation manager and/or runtime system may delay type inference on ageneric type parameter of an implementation of an overloaded function,in which the generic type parameter is associated with a type intervalcomprising an unbounded lower limit and one or more self-typedconstraints. Upon detecting a type query for a dynamic type of thegeneric type parameter, the compilation manager and/or runtime systemmay compare a queried type from the type query with a set of inferencechoices for the generic type parameter. If the queried type matches aninference choice from the set of inference choices, the compilationmanager and/or runtime system may use the inference choice to performtype inference on the generic type parameter. If the queried type doesnot match any of the inference choices, the compilation manager and/orruntime system may continue to delay type inference on the generic typeparameter.

In addition, one or more components of computer system 500 may beremotely located and connected to the other components over a network.Portions of the present embodiments (e.g., compilation manager, runtimesystem, etc.) may also be located on different nodes of a distributedsystem that implements the embodiments. For example, the presentembodiments may be implemented using a cloud computing system thatremotely manages the development, compilation, and execution of softwareprograms.

The foregoing descriptions of various embodiments have been presentedonly for purposes of illustration and description. They are not intendedto be exhaustive or to limit the present invention to the formsdisclosed. Accordingly, many modifications and variations will beapparent to practitioners skilled in the art. Additionally, the abovedisclosure is not intended to limit the present invention.

What is claimed is:
 1. A computer-implemented method for facilitatingthe development and execution of a software program, comprising duringruntime of the software program: during runtime of the software program;delaying type inference on a generic type parameter of an implementationof an overloaded function that is invoked in the software program,wherein the generic type parameter is associated with a type intervalcomprising an unbounded lower limit and one or more self-typedconstraints; upon detecting a type query for a dynamic type of thegeneric type parameter, comparing a queried type from the type querywith a set of inference choices for the generic type parameter; and whenthe comparing indicates that the queried type matches an inferencechoice from the set of inference choices, using the inference choice toperform type inference on the generic type parameter; if the queriedtype does not match any of the inference choices, further delaying typeinference on the generic type parameter.
 2. The computer-implementedmethod of claim 1, wherein using the inference choice to perform typeinference on the generic type parameter involves: using the inferencechoice as an updated lower limit of the type interval; and choosing abinding for the generic type parameter based on the updated lower limit.3. The computer-implemented method of claim 2, wherein the binding isfurther chosen based on a set of constraints associated with the generictype parameter.
 4. The computer-implemented method of claim 2, whereinchoosing the binding for the generic type parameter based on the updatedlower limit involves: if the generic type parameter is not associatedwith a set of constraints, using the inference choice as the binding forthe generic type parameter.
 5. The computer-implemented method of claim1, wherein the set of inference choices is bounded by the type interval.6. The computer-implemented method of claim 1, wherein the type intervalfurther comprises an upper limit.
 7. A system for facilitating thedevelopment and execution of a software program, comprising: acompilation manager for the software program; and a runtime system forthe software program, wherein the compilation manager and the runtimesystem are configured to, during runtime of the software program: delaytype inference on a generic type parameter of an implementation of anoverloaded function that is invoked in the software program, wherein thegeneric type parameter is associated with a type interval comprising anunbounded lower limit and one or more self-typed constraints; upondetecting a type query for a dynamic type of the generic type parameter,compare a queried type from the type query with a set of inferencechoices for the generic type parameter; and when the comparing indicatesthat the queried type matches an inference choice from the set ofinference choices, use the inference choice to perform type inference onthe generic type parameter; if the queried type does not match any ofthe inference choices, the compilation manager and the runtime systemare further configured to: further delay type inference on the generictype parameter.
 8. The system of claim 7, wherein using the inferencechoice to perform type inference on the generic type parameter involves:using the inference choice as an updated lower limit of the typeinterval; and choosing a binding for the generic type parameter based onthe updated lower limit.
 9. The system of claim 8, wherein the bindingis further chosen based on a set of constraints associated with thegeneric type parameter.
 10. The system of claim 8, wherein choosing thebinding for the generic type parameter based on the updated lower limitinvolves: if the generic type parameter is not associated with a set ofconstraints, using the inference choice as the binding for the generictype parameter.
 11. The system of claim 7, wherein the set of inferencechoices is bounded by the type interval.
 12. The system of claim 7,wherein the type interval further comprises an upper limit.
 13. Acomputer-readable storage medium 2 storing instructions that whenexecuted by a computer cause the computer to perform a method forfacilitating the development and execution of a software program, themethod comprising: during runtime of the software program: duringruntime of the software program, delaying type inference on a generictype parameter of an implementation of an overloaded function that isinvoked in the software program, wherein the genetic type parameter isassociated with a type interval comprising an unbounded lower limit andone or more self-typed constraints; upon detecting a type query for adynamic type of the generic type parameter, comparing a queried typefrom the type query with a set of inference choices for the generic typeparameter; and when the comparing indicates that the queried typematches an inference choice from the set of inference choices, using theinference choice to perform type inference on the genetic typeparameter; if the queried type does not match any of the inferencechoices, further delaying type inference on the generic type parameter.14. The computer-readable storage medium of claim 13, wherein using theinference choice to perform type inference on the generic type parameterinvolves: using the inference choice as an updated lower limit of thetype interval; and choosing a binding for the generic type parameterbased on the updated lower limit.
 15. The computer-readable storagemedium of claim 14, wherein the binding is further chosen based on a setof constraints associated with the generic type parameter.
 16. Thecomputer-readable storage medium of claim 14, wherein choosing thebinding for the generic type parameter based on the updated lower limitinvolves: if the generic type parameter is not associated with a set ofconstraints, using the inference choice as the binding for the generictype parameter.
 17. The computer-readable storage medium of claim 13,wherein the set of inference choices is bounded by the type interval.